{extend name="platform/base" /} {block name="main"}
<div class="row padder-v">
	<div class="col-sm-4">
		<a href="{:__URL('PLATFORM_MAIN/Goods/addGoodsCategory')}"
			class="btn btn-sm btn-primary"><i class="fa fa-plus"></i> 添加商品分类</a>
	</div>
</div>
<section class="panel panel-default">
	<table class="table table-striped b-t b-light text-sm">
		<thead>
			<tr>
				<th width="10"></th>
				<th width="80">排序</th>
				<th class="center">商品分类</th>
				<th class="center">商品分类简称</th>
				<th class="center">关联类型</th>
				<th class="center">是否显示</th>
				<th class="center" width="100">操作</th>
			</tr>
		</thead>
		<tbody>
			{foreach name="category_list" item="v1"}
			<tr class="pid_0" style="height: 30px;">
				<td>{if condition="$v1['child_list'] neq array()"}<a
					href="javascript:;" onclick="tab_switch({$v1['category_id']})"
					class="tab_jia_{$v1['category_id']}" style="display: block;"><i
						class="fa fa-plus"></i></a><a href="javascript:;"
					onclick="tab_switch({$v1['category_id']})"
					class="tab_jian_{$v1['category_id']}" style="display: none;"><i
						class="fa fa-minus"></i></a> {/if}
				</td>
				<td><input type="text" class="form-control"
					fieldid="{$v1['category_id']}" fieldname="sort"
					value="{$v1['sort']}" size="1"></td>
				<td>
					<div class="col-md-12">
						<input type="text" class="form-control"
							fieldid="{$v1['category_id']}" fieldname="category_name"
							value="{$v1['category_name']}">
					</div>
				</td>
				<td>
					<input type="text" class="form-control" fieldid="{$v1['category_id']}" fieldname="short_name" value="{$v1['short_name']}">
				</td>
				<td class="center">
					<a>{$v1['attr_name']}</a>
				</td>
				<td class="center">{$v1['is_visible'] ? '是' : '否'}</td>
				<td class="center"><a
					href="{:__URL('PLATFORM_MAIN/goods/updategoodscategory?category_id='.$v1['category_id'])}">修改</a>
					<a href="javascript:void(0);"
					onclick="delCategory({$v1['category_id']})">删除</a></td>
			</tr>
			{foreach name="v1['child_list']" item="v2"}
			<tr class="pid_{$v1['category_id']}"
				style="height: 30px; display: none;">
				<td>{if condition="$v2['child_list'] neq array()"}<a
					href="javascript:;" onclick="tab_switch({$v2['category_id']})"
					class="tab_jian_{$v2['category_id']}" style="display: block;"><i
						class="fa fa-minus"></i></a><a href="javascript:;"
					onclick="tab_switch({$v2['category_id']})"
					class="tab_jia_{$v2['category_id']}" style="display: none;"><i
						class="fa fa-plus"></i></a> {/if}
				</td>
				<td><input type="text" class="form-control"
					fieldid="{$v2['category_id']}" fieldname="sort"
					value="{$v2['sort']}" size="1"></td>
				<td><div class="col-md-1" style="padding-right:0px;"></div>
					<div class="col-md-11">
						<input type="text" class="form-control"
							fieldid="{$v2['category_id']}" fieldname="category_name"
							value="{$v2['category_name']}">
					</div></td>

				<td><input type="text" class="form-control"
					fieldid="{$v2['category_id']}" fieldname="short_name"
					value="{$v2['short_name']}"></td>
				<td class="center">
					{$v2['attr_name']}
				</td>	
				<td class="center">{$v2['is_visible'] ? '是' : '否'}</td>
				<td class="center"><a
					href="{:__URL('PLATFORM_MAIN/goods/updategoodscategory?category_id='.$v2['category_id'])}">修改</a>
					<a href="javascript:void(0);"
					onclick="delCategory({$v2['category_id']})">删除</a></td>
			</tr>
			{foreach name="v2['child_list']" item="v3"}
			<tr class="pid_{$v2['category_id']} pid_{$v1['category_id']}"
				style="height: 30px; display: none;">
				<td><label></td>
				<td><input type="text" class="form-control"
					fieldid="{$v3['category_id']}" fieldname="sort"
					value="{$v3['sort']}" size="1"></td>
				<td>
					<div class="col-md-2"></div>
					<div class="col-md-10">
						<input type="text" class="form-control"
							fieldid="{$v3['category_id']}" fieldname="category_name"
							value="{$v3['category_name']}">
					</div>
				</td>

				<td><input type="text" class="form-control"
					fieldid="{$v3['category_id']}" fieldname="short_name"
					value="{$v3['short_name']}"></td>
				<td class="center">
					{$v3['attr_name']}
				</td>	
				<td class="center">{$v3['is_visible'] ? '是' : '否'}</td>
				<td class="center"><a
					href="{:__URL('PLATFORM_MAIN/goods/updategoodscategory?category_id='.$v3['category_id'])}">修改</a>
					<a href="javascript:void(0);"
					onclick="delCategory({$v3['category_id']})">删除</a></td>
			</tr>
			{/foreach} {/foreach} {/foreach}
		</tbody>
	</table>
</section>

{/block} {block name="script"}
<script type="text/javascript">
$("table input").change(function(){
	var fieldid = $(this).attr('fieldid');
	var fieldname = $(this).attr('fieldname');
	var fieldvalue = $(this).val();
	if(fieldvalue == ''){
		showTip("不能为空哦","warning");
	}else{
		$.ajax({
			type:"post",
			url:"{:__URL('PLATFORM_MAIN/goods/modifygoodscategoryfield')}",
			data:{'fieldid':fieldid,'fieldname':fieldname,'fieldvalue':fieldvalue},
			success: function (data) {
				if(data["code"]>0){
					showTip("操作成功","success");
				}else{
					showTip("操作失败","error");
				}
			}
		});
	}
});

	function tab_switch(module_id){
		if($(".pid_"+module_id).css('display') != 'none'){
			$(".tab_jian_"+module_id).hide();
			$(".tab_jia_"+module_id).show();
			$(".pid_"+module_id).hide(300);
		}else{
			$(".tab_jian_"+module_id).show();
			$(".tab_jia_"+module_id).hide();
			$(".pid_"+module_id).show(500);
		}
	}
	/* 删除分类 */
	function delCategory(category_id) {
		$( "#dialog" ).dialog({
			buttons: {
				"确定": function() {
					$(this).dialog('close');
					$.ajax({
						type : "post",
						url : "{:__URL('PLATFORM_MAIN/goods/deletegoodscategory')}",
						data : {
							'category_id' : category_id,
							'goods_category_quick' : JSON.stringify(getCookieGoodsCategory(category_id))
						},
						dataType : "json",
						success : function(data) {
							if(data['code'] > 0){
								showMessage('success', data["message"],"{:__URL('PLATFORM_MAIN/goods/goodscategorylist')}");
							}else{
								showMessage('error', data["message"]);
							}
						}
					});
				},
				"取消,#e57373": function() {
					$(this).dialog('close');
				}
			},
			contentText:"你确定删除吗？",
			title:"消息提醒"
		});
	}
	/**
	*	查询当前用户最近一天是否发布过商品，修改过商品分类，要删除cookie中对应的值
	*/
	function getCookieGoodsCategory(category_id){
		var goods_category_quick_new = new Array();// 快速选择商品分类集合
		$.ajax({
			url : "{:__URL('PLATFORM_MAIN/goods/getquickgoods')}",
			type : "post",
			async : false,
			success : function(res) {
				var goods_category_quick = eval(res);// 将Cookie中的数据取出来
				for (var i = 0; i < goods_category_quick.length; i++) {
					var quick_name = goods_category_quick[i]["quick_name"];
					var quick_id_arr = goods_category_quick[i]["quick_id"].split(",");
					if(quick_id_arr[quick_id_arr.length-1] == category_id){
						continue;
					}else{
						var json = {
							quick_name : quick_name,
							quick_id : goods_category_quick[i]["quick_id"],
						};
						goods_category_quick_new.push(json);
					}
				}
			}
		});
		return goods_category_quick_new;
	}
</script>
{/block}
